<template>
  <vue-content>
    <vue-header>
      <vue-header-form>
        <el-form :inline="true" :model="formFilter" ref="formFilter">
        	<el-form-item label="商品类型" prop="productType">
            <el-select v-model="formFilter.productType" style="width:120px">
              <el-option label="实物" value="1"></el-option>
              <el-option label="券/虚拟" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="商品名称" prop="productName">
            <el-input v-model.trim="formFilter.productName" style="width:150px"></el-input>
          </el-form-item>
          <el-form-item label="商品类别" prop="categoryNo">
            <!-- <el-select v-model="formFilter.categoryNo" style="width:150px">
              <el-option 
              	v-for="item in CategoryList"
      					:key="item.id"
      					:label="item.categoryName"
      					:value="item.categoryNo"
                  ></el-option>
            </el-select> -->
            <el-cascader
              style="width:150px"
              :options="CategoryList"
              :props="cascaderConfig"
              v-model="category"
              :change-on-select="ifSelect"
              @change="categoryChange">
            </el-cascader>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="SearchSubmit('formFilter')">查询</el-button>
            <el-button @click="resetForm('formFilter')">重置</el-button>
            <el-button @click="exportForm('formFilter')">导出</el-button>
          </el-form-item>
        </el-form>
      </vue-header-form>
    </vue-header>
    <vue-body>
      <el-table
      	v-loading.body="loading"
        :data="tableData"
        border
        style="width: 100%">
        <el-table-column
          prop="productName"
          label="商品名称">
        </el-table-column>
        <el-table-column
          prop="categoryName"
          label="商品分类名称">
        </el-table-column>
        <el-table-column
          prop="marketNumber"
          label="销售数量">
        </el-table-column>
        <el-table-column
          prop="totalNumber"
          label="商品总数量" v-if="ifShow">
        </el-table-column>
        <el-table-column
          prop="surplusNumber"
          label="剩余数量" v-if="ifShow">
        </el-table-column>
        <el-table-column
          prop="endDate"
          label="截止日期" v-if="ifShow">
        </el-table-column>
        <el-table-column
          prop="mchtName"
          label="商户名称">
        </el-table-column>
      </el-table>
    </vue-body>
     <vue-footer style="padding: 5px 0;margin: 0 -5px;float:right;">
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="10"
        layout="total, prev, pager, next"
        :total="pageSize">
      </el-pagination>
    </vue-footer>
    <!--<edit-role
      ref="edit-role"
      @close="editFinish"
      :view="view"
      v-if="view ==='create' || view ==='edit'"/>
    <edit-detail
      ref="edit-detail"
      @close="editFinish"
      :view="view"
      v-if="view ==='detail'"/>-->
  </vue-content>
</template>

<script>
  import { md5 } from '@/utilities/crypto' // 加密
  import AuthStorage from '@/services/storage.js'
  import {queryAllCategory} from '@/services/product'
  import {statisticsTotalProduct} from '@/services/reports'
  import {RequestError} from '@/services/errors'
  import {deepClone} from '@/utilities/object'
  import EditRole from './edit'
  import EditDetail from './Detail'
  export default {
    name: 'role-table',
    components: {EditRole,EditDetail},
    data: () => ({
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      formFilter: {
      	productType:"1",
        productName: "",
        categoryNo:""
      },
      loading: false,
      CategoryList:[],
      view: '',
      ifShow:false,
      cascaderConfig: {
				label: 'categoryName',
				value: 'categoryNo',
				children: 'secondLevel'
      },
      category:[],
      ifSelect:true
    }),
    methods: {
    	async loadCategory () {
        try {
          let data = await queryAllCategory()
          this.CategoryList = data
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      async loadData () {
        this.loading = true
        if(this.formFilter.productType == "1"){
        	this.ifShow = false
        }else{
        	this.ifShow = true
        }
        var categoryLength = this.category.length;
        this.formFilter.categoryNo = this.category[categoryLength-1];
        try {
          let data = await statisticsTotalProduct(Object.assign({currentPage: this.currentPage},{perPage:10},this.formFilter))
          // debugger
          this.tableData = data.data
          this.pageSize = parseInt(data.totalResult)
        } catch (e) {
          if (e instanceof RequestError) {
            this.$alert(e.message, '提示', {
              type: 'error',
              confirmButtonText: '确定'
            })
          } else {
            throw e
          }
        }
        this.loading = false
      },
      async exportData () {
        var param=Object.assign({currentPage: this.currentPage},{perPage:10},this.formFilter);
        var  a = new Date();
        var btime= a .getTime();
        var signs2 = "data="+JSON.stringify(param)+"&stmb="+btime;
        this.postDownLoadFile({
          url: this.$store.state.exportUrl + 'ecshop/console/statement/statisticsTotalProductExport.do?stmb='+ a.getTime() +'&source="03"&token='+ AuthStorage.token +'&version=""&data='+JSON.stringify(param)+'&sign='+this.signature(signs2),
          method:'post'
        });
        // this.postDownLoadFile({
        //   url:'http://192.168.2.175:8888/study-web/file/exportExcel',
        //   method:'post'
        // });
      },
      categoryChange(value){
        // console.log(value);
      },
      SearchSubmit () {
        this.loadData()
      },
      signature(data) {
        return md5(data)
      },
      resetForm (formName) {
        this.$refs[formName].resetFields()
        this.category = []
        this.currentPage = 1
        this.loadData()
      },
      exportForm(){
        this.exportData();
      },
      postDownLoadFile(options) {
          var config = $.extend(true, { method: 'post' }, options);
          var $iframe = $('<iframe id="down-file-iframe" />');
          var $form = $('<form target="down-file-iframe" method="' + config.method + '" />');
          $form.attr('action', config.url);
          for (var key in config.data) {
              $form.append('<input type="hidden" name="' + key + '" value="' + config.data[key] + '" />');
          }
          $iframe.append($form);
          $(document.body).append($iframe);
          $form[0].submit();
          $iframe.remove();
      }
    },
    watch: {
      currentPage (newVal) {
        this.loadData(newVal)
      }
    },
    filters: {
      productType (value) {
        return {1: '实物', 2: '彩票',3:'券/虚拟'}[value]
      },
      distributionStatus (value) {
      	//配送状态
        return {'01': '待发货', '02': '已发货'}[value]
      },
      payType (value) {
      	//支付方式
        return {'01': '微信支付', '02': '芸券支付','03':'兴业银行'}[value]
      },
      isLogistics(value) {
      	//是否需要物流
        return {'01': '需要', '02': '不需要'}[value]
      },
    },
    created () {
      this.loadData()
      this.loadCategory()
    }
  }
</script>
